import tkinter as tk
from tkinter.scrolledtext import ScrolledText

root = tk.Tk()
root.title("文本框滚动条示例")
root.geometry("300x300")
text_str=""""""
for i in range(300):
    text_str += f"这是第{i}行{'-'*130}\n"
def test1():
    child_window1 = tk.Toplevel()
    child_window1.title("带滚动条的文本框 ScrolledText")
    child_window1.geometry("333x400")
    text = ScrolledText(child_window1, height=30, width=50)
    text.pack()
    text.delete(1.0, tk.END)
    text.insert(tk.INSERT, text_str)

def test2():
    child_window2 = tk.Toplevel()
    child_window2.title("可改变大小的文本框")
    child_window2.geometry("333x400")
    frame = tk.Frame(child_window2)
    frame.pack(fill=tk.BOTH, expand=True)

    text = ScrolledText(frame, wrap=tk.WORD)
    text.pack(fill=tk.BOTH, expand=True)
    text.delete(1.0, tk.END)
    text.insert(tk.INSERT, text_str)


class ResizableFrame(tk.Frame):
    def __init__(self, master=None, **kw):
        tk.Frame.__init__(self, master, **kw)
        self.minsize = (100, 100)  # 最小尺寸
        self.bind("<Button-1>", self.start_move)
        self.bind("<B1-Motion>", self.on_motion)
        self.start_x = None
        self.start_y = None

    def start_move(self, event):
        self.start_x = event.x
        self.start_y = event.y

    def on_motion(self, event):
        dx = event.x - self.start_x
        dy = event.y - self.start_y
        x1, y1, x2, y2 = self.geometry().split('+')
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
        self.place(x=x1 + dx, y=y1 + dy, width=x2, height=y2)
        self.start_x = event.x
        self.start_y = event.y
def test3():
    child_window2 = tk.Toplevel()
    child_window2.title("可改变大小的文本框")
    child_window2.geometry("533x400")
    resizable_frame = ResizableFrame(child_window2)
    resizable_frame.place(x=0, y=0, width=400, height=300)
    text_area = ScrolledText(resizable_frame, wrap=tk.WORD)
    text_area.pack(fill=tk.BOTH, expand=True)
    # 插入一些文本到文本框中，以便可以滚动查看
    text_area.insert(tk.END, text_str)

def test4():
    child_window3 = tk.Toplevel()
    child_window3.title("可改变大小的文本框")
    child_window3.geometry("633x400")
    text_box = tk.Text(child_window3, wrap=tk.NONE)
    # 创建两个滚动条，一个横向一个纵向
    scrollbar_y = tk.Scrollbar(child_window3, orient=tk.VERTICAL, command=text_box.yview)
    scrollbar_x = tk.Scrollbar(child_window3, orient=tk.HORIZONTAL, command=text_box.xview)

    # 将滚动条与文本框关联
    text_box.configure(yscrollcommand=scrollbar_y.set)
    text_box.configure(xscrollcommand=scrollbar_x.set)

    # 将滚动条和文本框放置到窗口中
    text_box.grid(row=0, column=0, sticky='nsew')
    scrollbar_y.grid(row=0, column=1, sticky='ns')
    scrollbar_x.grid(row=1, column=0, sticky='ew')

    # 配置窗口网格属性，使文本框和滚动条能够自动调整大小
    child_window3.grid_rowconfigure(0, weight=1)
    child_window3.grid_columnconfigure(0, weight=1)

    # 插入一些文本到文本框中，以便可以滚动查看
    text_box.insert(tk.END, text_str)


download_button = tk.Button(root, text="带滚动条的文本框", command=test1)
download_button.pack()
download_button = tk.Button(root, text="可改变大小的文本框", command=test2)
download_button.pack()
download_button = tk.Button(root, text="可改变大小的文本框(自定义Frame)", command=test3)
download_button.pack()

download_button = tk.Button(root, text="横纵向滚动条文本框(tk.Text)", command=test4)
download_button.pack()
root.mainloop()
